user_list1 = ['123', 'swim']
user_list2 = ['456', 'read']
root_list1 = ['123456', 'sleep']
user_namepwd = {'anna': user_list1, 'ben': user_list2}
root_namepwd = {'root': root_list1}
tplt = "{0:^10}\t{1:^10}\t{2:^10}"


# 登入界面
def menu():
    print("用户管理系统".center(15, '-'))
    while 1:
        print("1.登录".center(15))
        print("2.退出".center(15))
        select = input("按'q'退出".center(15))
        if select == 'q':
            print("系统即将退出...".center(15, '-'))
            exit()
        else:
            # 判断用户密码是否正确
            user_name = input("请输入用户名:")
            user_pwd = input("请输入密码:")
            if user_name in user_namepwd and user_pwd == user_namepwd[user_name][0]:
                return user_name
            elif user_name in root_namepwd and user_pwd == root_namepwd[user_name][0]:
                return user_name
            else:
                print("输入错误!请重新输入!".center(15, '-'))


# 普通用户菜单
def user_main(name):
    while 1:
        print("普通用户菜单".center(15, '-'))
        print("1.修改当前爱好".center(15))
        print("b.返回上一级菜单".center(15))
        print("q.退出".center(15))
        select = input("按'q'退出,按'b'返回上级,任意键继续".center(15))
        if select == 'q':
            print("系统即将退出...".center(15, '-'))
            exit()
        elif select == 'b':
            print("系统即将返回上级...".center(15))
            name = menu()
            if name in user_namepwd:
                user_main(name)
            else:
                root_main(name)
        else:
            print("修改当前爱好".center(15, '-'))
            print("用户当前信息为：")
            tplt = "{0:^10}\t{1:^10}\t{2:^10}"
            print(tplt.format("用户", "密码", "爱好"))
            print(tplt.format(name, user_namepwd[name][0], user_namepwd[name][1]))
            hobby = input("请输入新的爱好")
            user_namepwd[name][1] = hobby
            print(tplt.format("用户", "密码", "爱好"))
            print(tplt.format(name, user_namepwd[name][0], user_namepwd[name][1]))


# 管理员菜单
def root_main(name):
    while 1:
        print("管理员菜单".center(15, '-'))
        print("1.查看当前用户".center(15))
        print("2.修改某位用户".center(15))
        print("3.删除某位用户".center(15))
        print("4.添加某位用户".center(15))
        print("b.返回上一级菜单".center(15))
        print("q.退出".center(15))
        select = input("按'q'退出,按'b'返回上级,对应键继续".center(15))
        if select == 'q':
            print("系统即将退出...".center(15, '-'))
            exit()
        elif select == 'b':
            print("系统即将返回上级...".center(15))
            name = menu()
            if name in user_namepwd:
                user_main(name)
            else:
                root_main(name)

        # 查看某位用户
        elif select == '1':
            print("查看当前用户".center(15, '-'))
            print(tplt.format("用户", "密码", "爱好"))
            for i in user_namepwd:
                print(tplt.format(i, user_namepwd[i][0], user_namepwd[i][1]))
            print("即将返回本菜单首页".center(15, '-'))

        # 修改某用户
        elif select == '2':
            change_username = input("请输入要修改的用户名:")
            if change_username in user_namepwd:
                print("当前用户名信息为:")
                print(tplt.format("用户", "密码", "爱好"))
                print(tplt.format(change_username, user_namepwd[change_username][0], user_namepwd[change_username][1]))
                change_pwd = input("请输入要修改的密码:")
                change_hobby = input("请输入要修改的爱好:")
                user_namepwd[change_username][0] = change_pwd
                user_namepwd[change_username][1] = change_hobby
                print("修改后用户名信息为:")
                print(tplt.format("用户", "密码", "爱好"))
                print(tplt.format(change_username, user_namepwd[change_username][0], user_namepwd[change_username][1]))
            else:
                print("用户名有误!请重新输入!".center(15, '-'))

        # 删除某用户
        elif select == '3':
            del_username = input("请输入要删除的用户的用户名")
            if del_username in user_namepwd:
                user_namepwd.pop(del_username)
                print("删除成功!")
            else:
                print("此用户名不存在!")

        # 添加某用户
        elif select == '4':
            add_username = input("请输入要添加的用户的用户名")
            if add_username in user_namepwd:
                print("此用户名已存在!")
            else:
                add_userpwd = input("请输入新用户密码")
                add_userhobby = input("请输入新用户新爱好")
                add_userlst = (add_userpwd, add_userhobby)
                rights = input("请输入权限(0/1)(普通用户/管理员)")
                if rights == '0':
                    user_namepwd[add_username] = add_userlst
                    print("新用户名信息为:")
                    print(tplt.format("用户", "密码", "爱好"))
                    print(tplt.format(add_username, user_namepwd[add_username][0], user_namepwd[add_username][1]))
                else:
                    root_namepwd[add_username] = add_userlst
                    print("新管理员信息为:")
                    print(tplt.format("用户", "密码", ""))
                    print(tplt.format(add_username, root_namepwd[add_username][0], ""))


name = menu()
if name in user_namepwd:
    user_main(name)
else:
    root_main(name)
